package cn.itcast.mq.listener;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.time.LocalTime;
import java.util.Map;

@Component
public class SpringRabbitListener {

    @RabbitListener(queues = "basic.queue")
    public void listenerBasicQueue1(Map<String,Object> msg) throws InterruptedException {
        System.out.println("spring消费者1接收到的消息:"+msg+ LocalTime.now());
        Thread.sleep(20);
    }

    @RabbitListener(queues = "basic.queue")
    public void listenerBasicQueue2(String msg) throws InterruptedException {
        System.out.println("spring消费者2接收到的消息:"+msg+LocalTime.now());
        Thread.sleep(50);
    }

    @RabbitListener(queues = "fanout.queue1")
    public void listenerfanoutQueue1(String msg){
        System.out.println("fanout1消费者接收到消息:"+msg);
    }

    @RabbitListener(queues = "fanout.queue2")
    public void listenerfanoutQueue2(String msg){
        System.out.println("fanout2消费者接收到消息:"+msg);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue1"),
            exchange = @Exchange(name = "itcast.direct",type = ExchangeTypes.DIRECT),
            key = {"red","blue"}
            ))
    public void listenerdirectQueue1(String msg){
        System.out.println("direct1消费者接收到消息:"+msg);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue2"),
            exchange = @Exchange(name = "itcast.direct",type = ExchangeTypes.DIRECT),
            key = {"red","yellow"}
    ))
    public void listenerdirectQueue2(String msg){
        System.out.println("direct2消费者接收到消息"+msg);
    }

    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(name = "itcast.topic",type = ExchangeTypes.TOPIC),
            value = @Queue(name = "topic.queue1"),

            key = "china.#"
    ))
    public void listenertopicQueue1(String msg){
        System.out.println("topic1消费者接收到消息:"+msg);
    }


    @RabbitListener(bindings = @QueueBinding(
            exchange =@Exchange(name = "itcast.topic",type = ExchangeTypes.TOPIC),
            value = @Queue(name = "topic.queue2"),
            key = "#.news"
    ))
    public void listenertopicQueue2(String msg){
        System.out.println("topic2消费者接收到消息:"+msg);
    }

}
